import json

# 初始化任务列表
tasks = []

# 加载任务数据
def load_tasks():
    global tasks
    try:
        with open("tasks.json", "r") as file:
            tasks = json.load(file)
    except FileNotFoundError:
        tasks = []

# 保存任务数据
def save_tasks():
    with open("tasks.json", "w") as file:
        json.dump(tasks, file)

# 添加任务
def add_task():
    name = input("请输入任务名称：")
    deadline = input("请输入截止日期（YYYY-MM-DD）：")
    priority = input("请输入优先级（高/中/低）：")
    task = {"name": name, "deadline": deadline, "priority": priority}
    tasks.append(task)
    print("任务已添加！")

# 查看任务
def view_tasks():
    if not tasks:
        print("当前没有任务。")
    else:
        for i, task in enumerate(tasks):
            print(f"{i + 1}. 名称：{task['name']}，截止日期：{task['deadline']}，优先级：{task['priority']}")

# 主菜单
def main_menu():
    while True:
        print("\n=== 个人日程管理器 ===")
        print("1. 添加任务")
        print("2. 查看任务")
        print("3. 退出")
        choice = input("请选择操作：")
        if choice == "1":
            add_task()
        elif choice == "2":
            view_tasks()
        elif choice == "3":
            save_tasks()
            print("程序已退出。")
            break
        else:
            print("无效选择，请重试。")

# 主程序入口
if __name__ == "__main__":
    load_tasks()
    main_menu()
